<?php
/**
 * Created by PhpStorm.
 * User: yangyou
 * Date: 2018/11/30 0030
 * Time: 14:01
 */

namespace app\index\controller;
use app\index\model\Browserecord;
use app\index\model\Customized;
use app\index\model\Order;
use app\index\model\User;
use app\index\model\UserAddress;
use app\index\model\UserCookie;
use think\Config;

class users extends Base
{
    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:用户订单记录
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderRecord()
    {
        $user_info = session("user_info");
        $Order = new Order();
        $orderList = $Order->orderList($user_info["user_id"]);
        return view('user\orderRecord',[
            "orderList" => $orderList
        ]);
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:用户收货地址
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function receiverAddress()
    {
        $UserAddress = new UserAddress();
        $user_id = session("user_info")["user_id"];
        $addressList = $UserAddress->addressList($user_id);
        Config::load(APP_PATH.'admin/config.php');//加载上传配置文件
        $ImgApiData = config("ImgApiData");
        $shengList = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressList"], ["pid"=>0]);
        $shengList = json_decode($shengList,true);
        if(!empty($addressList)){
            foreach ($addressList as $key=>$val){
                $shengInfo = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressInfo"], ["id"=>$val["sheng"]]);
                $shengInfo = json_decode($shengInfo,true);
                $addressList[$key]["sheng_name"] = $shengInfo["data"]["name"];
                $shiInfo = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressInfo"], ["id"=>$val["shi"]]);
                $shiInfo = json_decode($shiInfo,true);
                $addressList[$key]["shi_name"] = $shiInfo["data"]["name"];
                $xianInfo = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressInfo"], ["id"=>$val["xian"]]);
                $xianInfo = json_decode($xianInfo,true);
                $addressList[$key]["xian_name"] = $xianInfo["data"]["name"];
            }
        }
        return view('user\receiverAddress',[
            "shengList" => $shengList["data"],
            "addressList" => $addressList
        ]);
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:地址(省，市，区)
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addressList()
    {
        $id = input('id',0);
        Config::load(APP_PATH.'admin/config.php');//加载上传配置文件
        $ImgApiData = config("ImgApiData");
        $res = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressList"], ["pid"=>$id]);
        echo $res;
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:收货地址
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addAddressInfo()
    {
        $name = input('name/s');
        $tel = input('tel/s');
        $sheng = input('sheng/d');
        $shi = input('shi/d');
        $xian = input('xian/d');
        $address = input('address/s');
        $check_address = input('check_address',0);
        $UserAddress = new UserAddress();
        $user_id = session("user_info")["user_id"];
        if ($check_address){
            $UserAddress->setFieldValue("default","1","default","0");//将之前设置为默认地址的取消
        }
        $result = $UserAddress->mySave(["tel"=>$tel,"nick_name"=>$name,"sheng"=>$sheng,"shi"=>$shi,"xian"=>$xian,"address"=>$address,"default"=>$check_address,"user_id"=>$user_id]);
        echo $result;
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:收货地址
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function deleteAddress()
    {
        $address_id = input('address_id/d');
        $UserAddress = new UserAddress();
        $result = $UserAddress->del("address_id" ,$address_id,true);
        echo $result;
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:修改地址
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editAddress()
    {
        $id = input('id/d');
        $UserAddress = new UserAddress();
        $addressInfo = $UserAddress->dataInfo("address_id",$id);
        Config::load(APP_PATH.'admin/config.php');//加载上传配置文件
        $ImgApiData = config("ImgApiData");
        $shengInfo = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressInfo"], ["id"=>$addressInfo["sheng"]]);
        $shengInfo = json_decode($shengInfo,true);
        $shiInfo = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressInfo"], ["id"=>$addressInfo["shi"]]);
        $shiInfo = json_decode($shiInfo,true);
        $xianInfo = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressInfo"], ["id"=>$addressInfo["xian"]]);
        $xianInfo = json_decode($xianInfo,true);
        $shengList = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressList"], ["pid"=>0]);
        $shengList = json_decode($shengList,true);
        return view('user\editAddress',[
            "shengList" => $shengList["data"],
            "shengInfo" => $shengInfo["data"],
            "shiInfo" => $shiInfo["data"],
            "xianInfo" => $xianInfo["data"],
            "addressInfo" => $addressInfo
        ]);
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:修改提交收货地址
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editAddressInfo()
    {
        $id = input('address_id/d');
        $name = input('name/s');
        $tel = input('tel/s');
        $sheng = input('sheng/d');
        $shi = input('shi/d');
        $xian = input('xian/d');
        $address = input('address/s');
        $check_address = input('check_address',0);
        $UserAddress = new UserAddress();
        if ($check_address){
            $UserAddress->setFieldValue("default","1","default","0");//将之前设置为默认地址的取消
        }
        $result = $UserAddress->mySave(["tel"=>$tel,"nick_name"=>$name,"sheng"=>$sheng,"shi"=>$shi,"xian"=>$xian,"address"=>$address,"default"=>$check_address],"address_id",$id);
        echo $result;
    }

    /**
     * Author:yang you
     * Time:2018-01-03 10:00:43
     * Describe:用户浏览记录
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function browseRecord()
    {
        $user_info = session('user_info');
        $User = new UserCookie();
        $browseList = $User->browseList($user_info["user_id"],"drawing_id");
        return view('user\browseRecord',[
            "browseList" => $browseList,
            'page' => $browseList->render(),
            ]);
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:删除浏览记录
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function deleteBrowse()
    {
        $browse_id = input('browse_id/d');
        $BrowseRecord = new Browserecord();
        $result = $BrowseRecord->del("browse_id" ,$browse_id,true);
        echo $result;
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:用户资料
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userInfo()
    {
        $user_info = session('user_info');
        $user = new User();
        $userInfo = $user->info($user_info['user_id']);

        return view('user\userInfo',[
            'userInfo' => $userInfo,
        ]);
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:修改用户资料
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editUserInfo()
    {
        $user_info = session('user_info');
        $user = new User();
        $userInfo = $user->info($user_info['user_id']);

        return view('user\editUserInfo',[
            'userInfo' => $userInfo,
        ]);
    }

    public function editUser()
    {
        $user_id = input('user_id/d');
        $nick_name = input('nick_name/s');
        $user = new User();
        $result = $user->mySave(['nick_name'=>$nick_name],'user_id',$user_id);
        echo $result;
    }

    /**
     * Author:yang you
     * Time:2018-11-28 10:00:43
     * Describe:用户图片上传
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function uploadImg()
    {
        $user_info = session('user_info');
        \think\Config::load(APP_PATH.'extra/upload.php');//加载上传配置文件
        $UploadFileConfig = \think\Config::get("UploadFileConfig");//获取配置
        $type = "UserPhoto";
        if(!empty($UploadFileConfig[$type])){
            $configImg = $UploadFileConfig[$type];//设置上传类型,有传入以传入为准
        }else{
            $result['code'] = 2;
            $result['msg'] = '没有该上传类型！';
        }
        if (!empty($configImg)) {//上传文件类型是否存在
            $validate = $configImg["validate"];
            $file = request()->file('file');
            $info = $file->validate($validate)->move($configImg["path"]);
            if ($info) {
                $user = new User();
                $getSaveName = $info->getSaveName();//获取图片路径
                $getSaveName = str_replace("\\", '/', $getSaveName);//替换所有的\
                $result['code'] = 0;
                $result['msg'] = '上传成功！';
                $result['url'] = "/".$configImg["path"] . $getSaveName;
                $result['name'] = $info->getFilename();
                $result['ext'] = $info->getExtension();
                $user->mySave(['head_portrait'=>$result['url']],'user_id',$user_info['user_id']);
            } else {
                $result['code'] = 1;
                $result['msg'] = $file->getError();
            }
        } else {
            $result['code'] = 2;
            $result['msg'] = '设置上传类型出错！';
        }

        return json($result);
    }

}